package com.qdevelop.model.service.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import com.qdevelop.model.utils.UtilsFactory;

public class SocketClient {
	public static SocketClient getInstance(){
		return new SocketClient();
	}

	public String sorketClient(String socketConfig,String requestParam){
		Socket socket = null;
		try{
			String _config = UtilsFactory.propertiesUtils().getProperty(socketConfig);
			if(_config==null||_config.indexOf(":")==-1) return null;
			socket = new Socket(_config.substring(0,_config.indexOf(":")).trim(), Integer.parseInt(_config.substring(_config.indexOf(":")+1).trim()));
			BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); 
			PrintWriter out=new PrintWriter(socket.getOutputStream()); 
			out.println(requestParam); 
			out.flush(); 
			String retuStr = in.readLine();
			return retuStr;
		}catch(Exception e){
			return null;
		}finally{
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) throws IOException {
		
	}

}
